import os
import re
import sys
import ConfigParser
import re

EXCLUSIONS_GROUP = ['com.lianye']
MVN_DEPENDENCY_COMMAND = 'mvn dependency:tree --> dependency.txt'
CONFIG = 'project.conf'

   
class GroupsGenerater:
   
    __patternStart = r'\[.*-\s'
    __patternEnd = r':.*$'
    __patternSpace = r'\s' 
    
    def __init__(self,inPath,outPath):
        self.inPath = inPath;
        self.outPath = outPath;
    def writeGroups(self):
        inFile = open(self.inPath,"r");
        fout = open(self.outPath,"w")  
        line = inFile.readline()
        initArray = []
        while line:
            initArray.append(line)
            line = inFile.readline()
        
        inFile.close()
        
        clearArray = []
        
        for jarName in initArray:
            temp = re.sub(GroupsGenerater.__patternStart,'',jarName)
            temp = re.sub(GroupsGenerater.__patternEnd,'',temp)
            temp = re.sub(GroupsGenerater.__patternSpace,'',temp)
            if temp in EXCLUSIONS_GROUP:
                continue
            clearArray.append(temp);
        uniqueGroups = set(clearArray);
        for groupName in uniqueGroups:
            fout.write(groupName+',\n');
            
class ConfigManager:
    
    def __init__(self, section):
        self.cf = ConfigParser.ConfigParser()
        self.cf.read(CONFIG)
        self.section = section
    @property
    def projectRoot(self):
        return self.cf.get(self.section, 'prject_root')
    
if __name__ == '__main__':
    clz = ConfigManager('backstage')
    print clz.projectRoot
    
class Command:
    
    def __init__(self):
        print "init command"
    def createDependencyFile(self):
        os.popen(MVN_DEPENDENCY_COMMAND) 
        
        
if __name__=="__main__":  
    
    configManager = ConfigManager('weight')
    projectRoot = configManager.projectRoot
    
    inPath = projectRoot+"\dependency.txt"
    outPath = projectRoot+"\dependencyGroup.txt"
    
    os.chdir(projectRoot)
     
    #command = Command()
    #command.createDependencyFile()
    
    groupsGenerater = GroupsGenerater(inPath,outPath)
    groupsGenerater.writeGroups()
    
    